
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min; //不含最大值，含最小值
}

function lastRandom(min, max) {
  return (Math.random() * (max - min) + min).toFixed(2); //不含最大值，含最小值


}

var gaugeData = [
  { name: '温度', value: 35, unit: '℃', pos: ['16.6%', '25%'], range: [34, 36], splitNum: 4 },
  { name: '湿度', value: 80, unit: '%', pos: ['55%', '25%'], range: [70, 95], splitNum: 5 },
  { name: 'co2浓度', value: 15, unit: '%', pos: ['16.6%', '75%'], range: [0, 20], splitNum: 4 },
  { name: '风速', value: 0.40, unit: 'm/s', pos: ['55%', '75%'], range: [0.35, 0.53], splitNum: 4 }

  //       { name: '电压', value: 220, unit: 'V', pos: ['16.6%', '25%'], range: [0, 400] },
  //   { name: '电流', value: 32, unit: 'A', pos: ['49.8%', '25%'], range: [0, 60] },
  //   { name: '功率因数', value: 0.9, pos: ['83%', '25%'], range: [0.1, 1.0], splitNum: 9 },
  //   { name: '有功功率', value: 6.34, unit: 'kW', pos: ['16.6%', '75%'], range: [0, 50] },
  //   { name: '有功电能', value: 6.28, unit: 'kWh', pos: ['49.8%', '75%'], range: [0, 50] },
  //   { name: '电网频率', value: 50, unit: 'Hz', pos: ['83%', '75%'], range: [0, 100] }
];



function gaugeMap(id, demoData) {
  const chartDom = document.getElementById(`${id}`);
  const myChart = echarts.init(chartDom);


  const setData = function() {
    return option = {
      series: (function() {
        const result = demoData.map(function(item, index) {
          // 内侧指针、数值显示
          let value = getRandomInt(item.range[0], item.range[1]);

          if (index === 3) {
            value = lastRandom(item.range[0], item.range[1])
          }
          return {
            splitNumber: item.splitNum || 10,
            splitLine: {
              show: false,
              width: 0
            },
            type: 'gauge',
            center: item.pos,
            radius: '33.33%',  // 1行3个
            startAngle: 225,
            endAngle: -45,
            min: item.range[0],
            max: item.range[1],
            axisLine: {
              show: true,
              lineStyle: {
                color: [[0.1, '#fc8452'], [0.9, '#3ba272'], [1, '#ff1f1f']], //仪表盘的轴线可以被分成不同颜色的多段。
                width: 8,//轴线宽度
              }
            },
            pointer: {
              length: '65%',
              width: 2,
              offsetCenter: [0, '5%'],
              itemStyle: {
                color: 'inherit'
              }
            },
            axisTick: {
              distance: -30,
              length: 8,
              lineStyle: {
                color: '#fff',
                width: 1
              }
            },
            itemStyle: {
              color: '#6ec3ff',
            },
            anchor: {
              show: true,
              showAbove: true,
              size: 35,
              itemStyle: {
                borderWidth: 10
              }
            },
            anchor: {
              show: true,
              showAbove: true,
              size: 35,
              itemStyle: {
                borderWidth: 10
              }
            },
            axisLabel: {
              distance: -20,
              textStyle: {
                color: '#ccc',
                fontSize: '10',
                fontWeight: 'normal'
              }
            },

            title: {//显示标题
              show: false,

            },
            detail: {
              show: true,
              offsetCenter: [0, '100%'],
              textStyle: {
                fontSize: 14,
                color: '#f95356'
              },
              formatter: [
                '{name|' + item.name + '}',
                '{value} ' + (item.unit || '')
              ].join('\n'),

              rich: {
                name: {
                  fontSize: 12,
                  lineHeight: 25,
                  color: '#6ec3ff'
                }
              }
            },

            data: [{
              name: item.name,
              value
            }]
          }
        });
        return result;
      })()
    };
  }

  myChart.setOption(setData());
  window.addEventListener("resize", function() {
    myChart.resize();
  });

  const chartoption = function() {
    myChart.setOption(setData());
  }

  setInterval(chartoption, 3000)
}
